package com.hafiery.sdk.bluetooth.test;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.hafiery.sdk.bluetooth.api.BlueToothSDK;
import com.hafiery.sdk.bluetooth.api.IBlueToothApi;
import com.hafiery.sdk.bluetooth.test.adapter.FileListAdapter;
import com.hafiery.sdk.bluetooth.test.databinding.ActivityFilelistBinding;
import com.hafiery.sdk.bluetooth.transport.adapter.Files;
import com.hafiery.sdk.bluetooth.transport.callback.Callback;
import com.hafiery.sdk.bluetooth.transport.constant.ErrorCode;
import com.hafiery.sdk.bluetooth.util.LogUtil;
import java.util.ArrayList;
import java.util.List;

public class FileListActivity extends AppCompatActivity {
    private ActivityFilelistBinding binding;
    private FileListAdapter adapter;
    private List<Files.RecordFile> mDatas=new ArrayList<>();
    private IBlueToothApi api;
    private int userPage =0;
    private int pageSize= 5;
    private String dir;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api= BlueToothSDK.instance().getBlueToothApi();
        binding=DataBindingUtil.setContentView(this,R.layout.activity_filelist);
        binding.toolbar.setTitle(getString(R.string.record_list));
        setSupportActionBar(binding.toolbar);
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
        adapter=new FileListAdapter(mDatas);
        adapter.setOnItemClicklistener((item, position) -> {
            LogUtil.log("索引:"+position);
            new AlertDialog.Builder(FileListActivity.this)
                    .setTitle("提示")
                    .setMessage("是否删除"+item.getName()+"?")
                    .setNegativeButton("取消", (dialog, which) -> {
                    }).setPositiveButton("确定", (dialog, which) -> {
                        api.removeRecordFile(dir+"/"+item.getName(), new Callback<String>() {
                            @Override
                            public void onSuccess(String data) {
                                Toast.makeText(FileListActivity.this,"删除文件成功",Toast.LENGTH_SHORT).show();
                                mDatas.remove(position);
                                adapter.notifyDataSetChanged();
                            }
                            @Override
                            public void onFailure(ErrorCode errorCode) {
                                Toast.makeText(FileListActivity.this,"删除文件失败",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }).show();
        });
        binding.recyclerview.setAdapter(adapter);

        binding.smartRefresh.setOnRefreshListener(refreshLayout -> {
            get(0);
        });
        binding.smartRefresh.setOnLoadMoreListener(refreshLayout -> {
            get(userPage+1);
        });
//        binding.smartRefresh.autoRefresh();

        EditText pageSizeEdit = new EditText(FileListActivity.this);
        pageSizeEdit.setPadding(50,20,50,20);
        pageSizeEdit.setHint("请输入pageSize");
        pageSizeEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
        new AlertDialog.Builder(FileListActivity.this)
                .setTitle("提示")
                .setMessage("请输入pageSize信息")
                .setView(pageSizeEdit)
                .setCancelable(false)
                .setNegativeButton("取消", (dialog, which) -> {
                })
                .setPositiveButton("确定", (dialog, which) -> {
                    String pageSize=pageSizeEdit.getText().toString();
                    if(TextUtils.isEmpty(pageSize)){
                        Toast.makeText(FileListActivity.this,"请输入正确的分页信息",Toast.LENGTH_LONG).show();
                    }else{
                        int p=0;
                        try {
                            p=Integer.parseInt(pageSize);
                            if(p<1){
                                Toast.makeText(FileListActivity.this,"请输入正确的分页信息",Toast.LENGTH_LONG).show();
                                return;
                            }
                            this.pageSize =p;
                            binding.smartRefresh.autoRefresh();
                        }catch (Exception e){
                            Toast.makeText(FileListActivity.this,"请输入正确的分页信息",Toast.LENGTH_LONG).show();
                        }

                    }
                })
                .show();

    }
    private void get(int page){
        api.getRecordList(page, pageSize, new Callback<Files>() {
            @Override
            public void onSuccess(Files data) {
                if(TextUtils.isEmpty(dir)){
                    dir=data.getFoldername();
                }
                List<Files.RecordFile> recordFiles=data.getFilelist();
                if(page==0){
                    userPage=0;
                    binding.smartRefresh.finishRefresh();
                    mDatas.clear();
                }else{
                    if(!recordFiles.isEmpty()){
                        userPage+=1;
                    }
                    LogUtil.log("下一页:"+userPage);
                    binding.smartRefresh.finishLoadMore();
                }
                mDatas.addAll(recordFiles);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(ErrorCode errorCode) {
                LogUtil.log("获取数据失败:"+errorCode.getMessage());
                if(page==0){
                    binding.smartRefresh.finishRefresh();
                }else{
                    binding.smartRefresh.finishLoadMore();
                }
            }
        });
    }
}
